import Vue from 'vue'
import VueRouter from 'vue-router'
import stroe from '../store/index'

import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Staff from '../views/Staff.vue'
import Commodity from '../views/Commodity.vue'
import Sale from '../views/Sale.vue'
import Activity from '../views/Activity.vue'
import Loading from '../views/Loading.vue'
import Cash from '../views/Cash.vue'
import Off from '../views/404.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: to => {
            console.log(to);
            if (stroe.state.user.quanxian != "") {
                return '/home'
            } else {
                return '/login'
            }
        }
    }, {
        path: '/login',
        name: 'login',
        component: Login,
        meta: {
            isLogin: false,
        }
    }, {
        path: '/home',
        name: 'home',
        components: {
            default: Home
        },
        meta: {
            isLogin: true,
            rols: ["99", "1"]
        },
        children: [{
                path: '/home/staff', //员工
                name: 'staff',
                component: Staff,
                meta: {
                    isLogin: true,
                    rols: ["99"]
                }
            },

            {
                path: '/home/commodity', //商品
                name: 'commodity',
                component: Commodity,
                meta: {
                    isLogin: true,
                    rols: ["99"]
                }
            },
            {
                path: '/home/sale', //销售
                name: 'sale',
                component: Sale,
                meta: {
                    isLogin: true,
                    rols: ["99"]
                }
            },
            {
                path: '/home/activity', //活动
                name: 'activity',
                component: Activity,
                meta: {
                    isLogin: true,
                    rols: ["99"]
                }
            },
            {
                path: '/home/loading',
                name: 'loading',
                component: Loading,
                meta: {
                    isLogin: true,
                    rols: ["99"]
                }
            },
            {
                path: '/home/cash',
                name: 'cash',
                component: Cash,
                meta: {
                    isLogin: true,
                    rols: ["99", "1"]
                }
            },
            {
                path: '/home/404',
                name: '404',
                component: Off,
                meta: {
                    isLogin: true,
                    rols: ["99", "1"]
                }
            },
        ],
        redirect: to => {
            console.log(to);
            if (stroe.state.user.quanxian == 99) {
                return '/home/staff'
            } else {
                return '/home/cash'
            }
        }
    },

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
router.beforeEach((to, from, next) => {
    if (to.meta.isLogin == false) { //去登陆页面
        next()
    } else {
        // 这里即将要去的页面是需要登录判断的
        if (stroe.state.user.quanxian) {
            // 现在我们要处理的问题是  我需要将登录的状态或者是信息保存下来
            // 登录  
            // 在to.meta.rols这个数组里有没有stroe.state.user.quanxian值
            if (to.meta.rols.includes(stroe.state.user.quanxian)) {
                next()
            } else {
                // 让你去404的页面
                router.push('/home/404')
            }
            // 在next之前 我是不是应该还要判断一下你又没有进入的权限
            // next()
        } else {
            router.push('/login')
        }
    }
})

export default router